package kr.hkit.powerjava.chapter24.work2;

import java.util.Scanner;

public class ServiceStudent {
	private StudentDao dao;
	private Scanner sc = new Scanner(System.in);
	
	
	public ServiceStudent() {
		dao = new StudentDao();
		init();
	}

	public ServiceStudent(StudentDao dao) {
		this.dao = dao;
		init();
	}
	
	private void init() {
		dao.loadFile();
		prnMenu();
	}

	private void prnMenu() {
		Student std = null;
		int res;
		do {
			System.out.print("1. 입력\n2. 삭제\n3. 수정\n4.검색\n5.리스트보기\n6.종료\n");
			res = sc.nextInt();
			switch (res){
				case 1:
					std = inputStdInfo(true);
					dao.insertItem(std);
					break;
				case 2:
					dao.prnItem(null);
					System.out.println("삭제할 학생정보를 입력하시오(학번 성명)");
					std = inputStdInfo(false);
					dao.deleteItem(std);
					break;
				case 3:
					dao.prnItem(null);
					System.out.println("수정할 학생정보를 입력하시오(학번 성명 국어 영어 수학");
					std = inputStdInfo(true);
					dao.updateItem(std);
					break;
				case 4:
					std = inputStdInfo(false);
					dao.searchItem(std);
					break;
				case 5:
					break;
				case 6:
					break;
				default:
						
			}
		} while (res != 6);
	}

	private Student inputStdInfo(boolean gubun) {
		System.out.println("학번 성명");
		int stdNum = sc.nextInt();
		String stdName = sc.next();
		
		if (gubun) {
			System.out.print("국어 영어 수학");
			int kor = sc.nextInt();
			int eng = sc.nextInt();
			int math = sc.nextInt();
			return new Student(stdNum, stdName, kor, eng, math);
		} else {
			return new Student(stdNum, stdName);
		}	
	}

	
}
